<template>
  <div class="usersign-container nav-bar-top">
    <!-- 导航栏 -->
    <van-nav-bar class="nav-bar" title="每日签到" left-arrow @click-left="$router.back()"/>
    <div class="circle-bg">
      <van-cell class="sign-title" title="签到">
        <div slot="extra" class="sign-num">
          本月已签到<span>{{signCount}}</span>天
        </div>
      </van-cell>
      <Calendar
        :markDateMore="markDate"
        :agoDayHide="currentDate"
        :futureDayHide="currentDate"/>
    </div>
    <!-- 签到 -->
    <div class="sign-btn">
      <van-button class="btn" :disabled="signBtnStatus" block @click="sign">{{signText}}</van-button>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import constants from '@/config/constants'
import Calendar from 'vue-calendar-component';
import userApi from '@/api/userApi'

export default {
  components: {
    Calendar
  },
  name: 'userSign',
  data () {
    return {
      /* 签到按钮状态 */
      signBtnStatus: true,
      /* 签到文字 */
      signText: '',
      /* 签到状态 */
      markDate: [],
      /* 签到次数 */
      signCount: 0,
      currentDate: Math.round(new Date().getTime()/1000).toString()
    }
  },
  computed: {
    ...mapState(['user'])
  },
  created () {
    this.getDailySignList();
  },
  methods: {
    /* 获取签到记录 */
    async getDailySignList() {
      var year = this.$moment().get('year');
      var month = this.$moment().get('month')+1;
      var day = this.$moment().get('date');
      const result = await userApi.dailySignList({
        memberId: this.user.id,
        year: year,
        month: month
      })
			
			if(result.code == constants.SUCCESS){
				this.markDate = result.data;
				
				//判断签到状态，签到次数
				var signBtnStatus = false;
				var signCount = 0;
				result.data.forEach(item => {
				  if(item.date == year+"-"+month+"-"+day && item.className == "sign-yes"){
				    signBtnStatus = true;
				  }
				  if(item.className == "sign-yes"){
				    signCount += 1;
				  }
				})
				this.signCount = signCount;
				this.signBtnStatus = signBtnStatus;
				this.signText = signBtnStatus ? '今日已签到' : '签到';
			}
    },
    /* 签到 */
    async sign(){
      const result = await userApi.dailySign({memberId: this.user.id})
			if(result.code == constants.SUCCESS){
				this.getDailySignList();
				this.$toast.success("签到成功");
			}
    }
  }
}
</script>

<style lang="scss">
.usersign-container{
  padding-top: 5px;
  .wh_content_all{
    background-color: #fff !important;
    /deep/ .wh_content_item{
      color: #333;
    }
    /deep/ .wh_top_changge{
      padding: 10px 0px;
      li{
        color: #333;
        font-size: 15px;
      }
      li:nth-child(1){
        justify-content: flex-start;
        padding-left: 15px;
      }
      li:nth-child(2){
        justify-content: center;
      }
      li:nth-child(3){
        justify-content: flex-end;
        padding-right: 15px;
      }
    }
    /deep/ .wh_content{
      justify-content: space-between;
      padding: 0;
      width: 100%;
    }
    /deep/ .wh_jiantou1{
      width: 10px;
      height: 10px;
      border-top: 1px solid #e9b461;
      border-left: 1px solid #e9b461;
    }
    /deep/ .wh_jiantou2{
      width: 10px;
      height: 10px;
      border-top: 1px solid #e9b461;
      border-right: 1px solid #e9b461;
    }
    /deep/ .wh_content_item{
      .wh_isToday{
        background: linear-gradient(132deg,#f3dfb1,#f3dfb1,#ecbe60);
        color: #fff;
      }
    }
    .wh_item_date, .wh_top_tag{
      width: 25px;
      height: 25px;
    }
    .wh_top_tag{
      font-size: 14px;
    }
  }
  .sign-num{
    font-size: 13px;
    color: #999;
    span{
      color: #e3ad5b;
      margin: 0 5px;
    }
  }
  .sign-btn{
    margin-top: 15px;
    padding: 0px 10px;
    display: flex;
    justify-content: center;
  }
  .sign-yes{
    background: url('') no-repeat !important;
    background-size: 25px 25px !important;
    font-size:0;
    line-height: 0;
    overflow:hidden;
  }
  .sign-title{
    padding-left: 5px;
    padding-right: 5px;
  }
}
</style>
